package game.craft.model;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.util.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ccayirog
 * Date: Mar 28, 2011
 * Time: 12:38:20 AM
 * To change this template use File | Settings | File Templates.
 */
public class BoardItem extends AbstractItem implements Item {

    private Craft craft = new Craft();
    private java.util.List<Item> aliens;


    public BoardItem() {
        super("", 0, 0);
        craft = new Craft();
        initAliens();
    }

    public void move() {
        for (Item alien : aliens) {
            if (alien.isExist())
                alien.move();
        }
        craft.move();
    }

    public void keyPressed(KeyEvent e) {
        craft.keyPressed(e);
    }

    public void keyReleased(KeyEvent e) {
        craft.keyReleased(e);
    }

    public void draw(Graphics graphics, ImageObserver observer) {
        craft.draw(graphics, observer);
        for (Object alien : aliens) {
            ((Item) alien).draw(graphics, observer);
        }
        graphics.setColor(Color.WHITE);
        graphics.drawString("Aliens left: " + aliens.size(), 5, 15);
    }

    public void checkCollisions() {
        if (craft.isExist()) {

            Rectangle r3 = craft.getBounds();

            for (int j = 0; j < aliens.size(); j++) {
                Alien a = (Alien) aliens.get(j);
                if (a.isExist()) {
                    Rectangle r2 = a.getBounds();
                    if (r3.intersects(r2)) {
                        craft.setExist(false);
                        a.setExist(false);
                    }
                }
            }
            List<Item> ms = craft.getMissiles();

            for (int i = 0; i < ms.size(); i++) {
                Missile m = (Missile) ms.get(i);
                if (m.isExist()) {
                    Rectangle r1 = m.getBounds();

                    for (int j = 0; j < aliens.size(); j++) {
                        Alien a = (Alien) aliens.get(j);
                        if (a.isExist()) {
                            Rectangle r2 = a.getBounds();
                            if (r1.intersects(r2)) {
                                m.setExist(false);
                                a.setExist(false);
                            }
                        }
                    }
                }
            }
        }

    }


    public void initAliens() {
        aliens = new ArrayList();

        for (int i = 0; i < pos.length; i++) {
            aliens.add(new Alien(pos[i][0], pos[i][1]));
        }
    }

    private int[][] pos = {
            {2380, 29}, {2500, 59}, {1380, 89},
            {780, 109}, {580, 139}, {680, 239},
            {790, 259}, {760, 50}, {790, 150},
            {980, 209}, {560, 45}, {510, 70},
            {930, 159}, {590, 80}, {530, 60},
            {940, 59}, {990, 30}, {920, 200},
            {900, 259}, {660, 50}, {540, 90},
            {810, 220}, {860, 20}, {740, 180},
            {820, 128}, {490, 170}, {700, 30}
    };
}
